package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.exception.DataNotFoundException;
import com.itheima.common.exception.DuplicateDataException;
import com.itheima.mapper.CheckGroup_ItemMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckItemServiceimpl implements CheckItemService {
    @Autowired
    CheckItemMapper checkItemMapper;
    @Autowired
    CheckGroup_ItemMapper checkGroup_itemMapper;
//查询全部----------
    @Override
    public ArrayList<CheckItem> findAll() {

       return checkItemMapper.findAll();
    }
//添加检查项-------------------
    @Override
    public void additem(CheckItem checkItem) {
        // 检查项目编码是否重复
        CheckItem codeTest = new CheckItem();
        codeTest.setCode(checkItem.getCode());
        List<CheckItem> items = checkItemMapper.select(codeTest);
        if (!items.isEmpty()){
            throw new DuplicateDataException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        // 项目名称是否重复
        CheckItem nameTest = new CheckItem();
        nameTest.setName(checkItem.getName());
        items = checkItemMapper.select(nameTest);
        if (!items.isEmpty()){
            throw new DuplicateDataException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }

        checkItemMapper.additem(checkItem);
    }
//删除检查项------------
    @Override
    public void deleteById(Long id) {
        checkGroup_itemMapper.deleteByItemId(id);
        checkItemMapper.deleteByid(id);
    }

//查询检查项通过组别----------------
    @Override
    public ArrayList<Integer> getByGroupId(int  id) {

        return checkItemMapper.getByGroupId(id);
    }
//修改检查项---------
    @Override
    public void updateByItemId(CheckItem checkItem) {
        checkItemMapper.updateByItemId(checkItem);
    }
//检查项分页查询----------------
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }
//查询检查项通过检查项id---------------
    @Override
    public CheckItem findById(int id) {
        CheckItem checkItem= checkItemMapper.findById(id);
        if(checkItem==null){
            throw new DataNotFoundException(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return  checkItem;
    }
}
